#pragma once

#include "../comm/log.hpp"
#include "../comm/sqlManager.hpp"
struct Post {
    int post_id;           // 文章ID
    int user_id;           // 用户ID
    std::string content;   // 文章内容
    std::string date;      // 发布时间
    int view_count;        // 浏览量
    int like_count;        // 点赞数
    int comment_count;     // 评论数
    std::string category;  // 分类
    std::string tags;      // 标签
    std::string image_url;
};

using namespace ns_util;

class PostManager {
private:
    ns_sql::MySQLManager _dbManager;
    std::mutex _mutex;

public:
    PostManager(const std::string &host, const std::string &user, const std::string &password, const std::string &dbName) {
        LOG_DEBUG() << "Initializing PostManager with database: " << dbName;
        if (!_dbManager.openDatabase(host, user, password, dbName)) {
            LOG_ERROR() << "Failed to open database: " << _dbManager.getErrorMessage();
        }
    }

    ~PostManager() {
        _dbManager.closeDatabase();
    }

    // 添加文章
    bool addPost(const int &userId, const std::string &content, const std::string &category, const std::string &tags, const std::string &imageUrl = "") {
        std::unique_lock<std::mutex> lock(_mutex);
        LOG_DEBUG() << "Adding post for user " << userId;

        std::string query = "INSERT INTO posts (user_id, content, date, category, tags, image_url) VALUES (" +
                            std::to_string(userId) + ", '" + content + "', '" + TimeUtil::GetDateTime() + "', '" +
                            category + "', '" + tags + "', '" + imageUrl + "')";

        bool result = _dbManager.executeQuery(query);
        if (!result) {
            LOG_ERROR() << "Failed to add post: " << _dbManager.getErrorMessage();
            return false;
        }

        LOG_DEBUG() << "Post added successfully.";
        return true;
    }

    // 删除文章
    bool deletePost(const int &postId) {
        std::unique_lock<std::mutex> lock(_mutex);
        LOG_DEBUG() << "Deleting post with ID " << postId;

        std::string query = "DELETE FROM posts WHERE post_id = " + std::to_string(postId);

        bool result = _dbManager.executeQuery(query);
        if (!result) {
            LOG_ERROR() << "Failed to delete post: " << _dbManager.getErrorMessage();
            return false;
        }

        LOG_DEBUG() << "Post deleted successfully.";
        return true;
    }

    // 获取所有文章
    bool getAllPosts(std::vector<Post> &postList) {
        std::unique_lock<std::mutex> lock(_mutex);

        std::string query = "SELECT post_id, user_id, content, date, view_count, like_count, comment_count, category, tags, image_url FROM posts";

        ns_sql::MySQLManager::MySQLResult result = _dbManager.getResult(query);

        if (!result.next()) {
            LOG_ERROR() << "No posts found or query failed: " << _dbManager.getErrorMessage();
            return false;
        }

        do {
            try {
                Post post;
                post.post_id = result.getInt(0);
                post.user_id = result.getInt(1);
                post.content = result.getString(2).empty() ? "" : result.getString(2);
                post.date = result.getString(3).empty() ? "" : result.getString(3);
                post.view_count = result.getInt(4);
                post.like_count = result.getInt(5);
                post.comment_count = result.getInt(6);
                post.category = result.getString(7).empty() ? "" : result.getString(7);
                post.tags = result.getString(8).empty() ? "" : result.getString(8);
                post.image_url = result.getString(9).empty() ? "" : result.getString(9);

                postList.push_back(post);
            } catch (const std::exception &e) {
                LOG_ERROR() << "Error processing post data: " << e.what();
                return false;
            }
        } while (result.next());

        return true;
    }

    // 根据用户ID获取文章
    bool getPostByUserId(const int &userId, std::vector<Post> &postList) {
        std::unique_lock<std::mutex> lock(_mutex);

        std::string query = "SELECT post_id, user_id, content, date, view_count, like_count, comment_count, category, tags, image_url FROM posts WHERE user_id = " + std::to_string(userId);

        ns_sql::MySQLManager::MySQLResult result = _dbManager.getResult(query);

        if (!result.next()) {
            LOG_ERROR() << "No posts found for user " << userId << " or query failed: " << _dbManager.getErrorMessage();
            return false;
        }

        do {
            try {
                Post post;
                post.post_id = result.getInt(0);
                post.user_id = result.getInt(1);
                post.content = result.getString(2).empty() ? "" : result.getString(2);
                post.date = result.getString(3).empty() ? "" : result.getString(3);
                post.view_count = result.getInt(4);
                post.like_count = result.getInt(5);
                post.comment_count = result.getInt(6);
                post.category = result.getString(7).empty() ? "" : result.getString(7);
                post.tags = result.getString(8).empty() ? "" : result.getString(8);
                post.image_url = result.getString(9).empty() ? "" : result.getString(9);

                postList.push_back(post);
            } catch (const std::exception &e) {
                LOG_ERROR() << "Error processing post data: " << e.what();
                return false;
            }
        } while (result.next());

        return true;
    }
};
